<template>
  <el-row justify="center" style="margin-top: 50px">
    <el-space wrap>
      <span>手机号</span>
      <el-input
        v-model="mobile"
        style="width: 240px"
        placeholder="请输入手机号"
        :suffix-icon="Calendar"
      />
      <el-button type="primary" @click="handlerQuery">查询</el-button>
      <el-button type="success" @click="showAddDialog">添加</el-button>
    </el-space>
  </el-row>
  <el-dialog v-model="dialogFormVisible" title="添加用户" width="500">
    <el-form :model="form">
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-input v-model="form.account" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" :label-width="formLabelWidth">
        <el-input v-model="form.password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱" :label-width="formLabelWidth">
        <el-input v-model="form.mail" autocomplete="off" />
      </el-form-item>
      <el-form-item label="手机号" :label-width="formLabelWidth">
        <el-input v-model="form.mobile" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleAdd"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
  <el-row justify="center" style="margin-top: 50px">
    <el-table :data="tableData.data.records" stripe style="width: 90%; margin-top: 50px" border>
      <el-table-column label="用户名" width="180" align="center">
        <template #default="scope">
          <el-link type="primary" @click="gotoUserAddRoles(scope.row.usersId)">{{
            scope.row.account
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="password" label="密码" width="180" align="center" />
      <el-table-column prop="mail" label="邮箱" width="180" align="center" />
      <el-table-column prop="mobile" label="联系方式" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" :icon="Edit" circle @click="queryUserById(scope.row.usersId)" />
          <el-button
            type="danger"
            :icon="Delete"
            circle
            @click="handleDelete(scope.$index, scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
  </el-row>
  <el-row justify="center" style="margin-top: 50px">
    <el-pagination
      background
      layout="prev, pager, next"
      :total="tableData.data.total"
      :default-page-size="pageSize"
      @prev-click="prePage"
      @next-click="nextPage"
      @current-change="goto"
    ></el-pagination>
  </el-row>
</template>
<script setup>
import { users } from '../../../http'
import { ref, onMounted } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import router from '../../../router'
var pageNo = 1
var pageSize = 10
var doUpdate = false
var mobile = ref('')
var dialogFormVisible = ref(false)
var form = ref({
  account: '',
  mobile: '',
  password: '',
  mail: ''
})
var tableData = ref({
  code: 0,
  data: {
    records: [],
    total: 0,
    size: 2,
    current: 1,
    orders: [],
    optimizeCountSql: true,
    hitCount: false,
    countId: null,
    maxLimit: null,
    searchCount: true,
    pages: 15
  },
  msg: '执行成功'
})
onMounted(() => {
  handlerQuery()
})
const gotoUserAddRoles = (usersId) => {
  console.log(usersId)
  router.push({ path: '/userAddRoles', query: { id: usersId } })
}

const queryUserById = async (id) => {
  doUpdate = true
  var result = await users.queryUserById(id)
  if (result.code == 0) {
    dialogFormVisible.value = true
    form.value = result.data
  }
}
const showAddDialog = () => {
  doUpdate = false
  dialogFormVisible.value = true
}
const handleAdd = async () => {
  var result = {}
  if (doUpdate) {
    result = await users.updateUser(form.value)
    if (result.code == 0) {
      ElMessage.success('修改成功！')
      dialogFormVisible.value = false
      handlerQuery(pageNo, pageSize, mobile.value)
      form = ref({})
    }
  } else {
    result = await users.addUser(
      form.value.account,
      form.value.password,
      form.value.mail,
      form.value.mobile
    )
    if (result.code == 0) {
      ElMessage.success('添加成功！')
      dialogFormVisible.value = false
      handlerQuery(pageNo, pageSize, mobile.value)
      form = ref({})
    }
  }
}
const handleDelete = async (index, row) => {
  var result = await users.deleteUser(row.usersId)
  console.log(result)
  if (result.code == 0) {
    ElMessage.success('删除成功！')
    tableData.value.data.records.splice(index, 1)
  }
}
const handlerQuery = async () => {
  const result = await users.queryUserList(pageNo, pageSize, mobile.value)

  if (result.code == 0) {
    tableData.value = result
    console.log(tableData.value.data.total)
    console.log(tableData.value.data.size)
  }
}
const prePage = () => {
  pageNo--
  handlerQuery(pageNo, pageSize, mobile.value)
}
const nextPage = () => {
  pageNo++
  handlerQuery(pageNo, pageSize, mobile.value)
}
const goto = (no) => {
  pageNo = no
  handlerQuery(pageNo, pageSize, mobile.value)
}
</script>
